package com.zking.service.impl;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zking.dao.AnswerquestionsMapper;
import com.zking.dao.CollectsMapper;
import com.zking.dao.QuestionsMapper;
import com.zking.dao.UsersMapper;
import com.zking.model.Answerquestions;
import com.zking.model.AnswerquestionsExample;
import com.zking.model.Collects;
import com.zking.model.CollectsExample;
import com.zking.model.Page;
import com.zking.model.Questions;
import com.zking.model.QuestionsExample;
import com.zking.model.Users;
import com.zking.model.UsersExample;
import com.zking.service.QuestionService;
import com.zking.util.AppException;
@Service("questionService")
@Transactional(rollbackFor=Exception.class)
public class QuestionServiceImpl implements QuestionService{
	@Autowired
	private QuestionsMapper questionmapper;
	@Autowired
	private UsersMapper uMapper;
	@Autowired
	private AnswerquestionsMapper aMapper;
	@Autowired
	private CollectsMapper cMapper;	
	
	//用户提问的标题和内容存入数据库
	public int tquestion(Questions question) throws AppException{
		return questionmapper.insertSelective(question);
	}
	
	//显示所有的问题
	public List<Questions> showquestion() throws AppException{
		List<Questions> qlist=null;
		List<Answerquestions> alist=null;
		List<Collects> clist = null;
		List<Questions> list = null;
		String name="";
		Page page = new Page();
		try {
			QuestionsExample example=new QuestionsExample();
			qlist=questionmapper.selectByExample(example);
			//list = questionmapper.showAllQuestions();
			if(qlist.size()>0){
				
				//添加名字
				for(int i=0;i<qlist.size();i++){
					//判断qid
					AnswerquestionsExample example2=new AnswerquestionsExample();
					example2.createCriteria().andQIdEqualTo(qlist.get(i).getqId());
					//判断qid
					CollectsExample example3 = new CollectsExample();
					example3.createCriteria().andQIdEqualTo(qlist.get(i).getqId());
					
					alist=aMapper.selectByExample(example2);
					clist = cMapper.selectByExample(example3);
					name=uMapper.selectByPrimaryKey(qlist.get(i).getuId()).getuName();
					qlist.get(i).setuName(name);
					//回答的数量
					qlist.get(i).setAcount(alist.size());
					//收藏的数量
					qlist.get(i).setCcount(clist.size());
					qlist.get(i).setPmax(3);
					qlist.get(i).setTotal(qlist.size());
					qlist.get(i).setPstart(0);
					qlist.get(i).setInpage(1);
				}
				
				return qlist;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//通过问题ID查找问题的详细信息
	public Questions selectQuestionsByqid(int qId) throws AppException{
		try {
			return questionmapper.selectByPrimaryKey(qId);
		} catch (SQLException e) {
			throw new AppException("数据异常",e);
		}
	}
	
	//用户回答问题的内容存入数据库
	public int answerquestion(Answerquestions answerquestion)throws AppException{
		return aMapper.insertSelective(answerquestion);
	}
	
	//根据qid查找问题的回答信息 
	public List<Answerquestions> selectByqid(int qId) throws AppException{
		List<Answerquestions> alist = null;
		AnswerquestionsExample example = new AnswerquestionsExample();
		example.createCriteria().andQIdEqualTo(qId);
		try {
			alist = aMapper.selectByExample(example);
			if(alist.size()>0){
				//添加名字,状态
				for(int i=0;i<alist.size();i++){
					String name = uMapper.selectByPrimaryKey(alist.get(i).getuId()).getuName();
					int dostate = questionmapper.selectByPrimaryKey(alist.get(i).getqId()).getqDostate();
					alist.get(i).setuName(name);
					alist.get(i).setqDostate(dostate);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return alist;
	}
			
	//根据uId查找用户提过的问题
	public List<Questions> selectByuid(int uId)throws AppException{
		List<Questions> qlist = null;
		QuestionsExample example = new QuestionsExample();
		example.createCriteria().andUIdEqualTo(uId);
		try {
			qlist = questionmapper.selectByExample(example);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return qlist;
	}
	//验证用户是否已经收藏了问题
	public int collects (Collects collect)throws AppException{
		List<Collects> clist = null;
		try {
			CollectsExample example = new CollectsExample();
			example.createCriteria().andUIdEqualTo(collect.getuId()).andQIdEqualTo(collect.getqId());
			clist = cMapper.selectByExample(example);
			if(clist.size()>0){
				return 0;
			}else {
				return cMapper.insertSelective(collect);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
	}
	//查找用户所有收藏的问题
	public List<Collects> selectAllByuid(int uId)throws AppException{
		List<Collects> clist = null;
		String name = "";
		Date applytime = null;
		String title = "";
		CollectsExample example = new CollectsExample();
		example.createCriteria().andUIdEqualTo(uId);
		try {
			clist = cMapper.selectByExample(example);
			if(clist.size()>0){
				//添加名字
				for(int i=0;i<clist.size();i++){
					name = uMapper.selectByPrimaryKey(clist.get(i).getuId()).getuName();
					applytime = questionmapper.selectByPrimaryKey(clist.get(i).getqId()).getqApplytime();
					title = questionmapper.selectByPrimaryKey(clist.get(i).getqId()).getqTitle();
					clist.get(i).setuName(name);
					clist.get(i).setApplytime(applytime);
					clist.get(i).setTitle(title);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clist;
	}
	//根据cId来删除用户的收藏
	public int deletecollect(int cId)throws AppException{
		try {
			return cMapper.deleteByPrimaryKey(cId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
	}
	//根据qId来删除用户提过的问题
	public int deletequestion(int qId)throws AppException{
		try {
			return questionmapper.deleteByPrimaryKey(qId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
	}
	//根据aId找到被采纳的答案
	public Answerquestions selectanswer(int aId,int qId,int uId)throws AppException{
		Answerquestions answerquestion = new Answerquestions();
		try {
			answerquestion = aMapper.selectByPrimaryKey(aId);
			int dostate = questionmapper.selectByPrimaryKey(qId).getqDostate();
			String name = uMapper.selectByPrimaryKey(uId).getuName();
			answerquestion.setqDostate(dostate);
			answerquestion.setuName(name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
		return answerquestion;
	}
	
	//根据qId修改用户状态
	public int updateanswer(int qId,int qDostate)throws AppException{
		try {
			return questionmapper.updateDostate(qId, qDostate);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
	}
	//根据aId修改用户状态
		public int updateanswerdostate(int aId,int aDostate)throws AppException{
			try {
				return aMapper.updateaDostate(aId, aDostate);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new AppException("数据异常",e);
			}
		}
	//根据状态查找所以的问题
	public List<Questions> selectbydostate(int qDostate)throws AppException{
		List<Questions> qlist = null;
		QuestionsExample example = new QuestionsExample();
		example.createCriteria().andQDostateEqualTo(qDostate);
		try {
			qlist = questionmapper.selectByExample(example);
			if(qlist.size()>0){
				//添加名字
				for(int i=0;i<qlist.size();i++){
					String name=uMapper.selectByPrimaryKey(qlist.get(i).getuId()).getuName();
					qlist.get(i).setuName(name);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
		return qlist;
	}
	//根据qId查找所有的回答并计数
	public int selectByqidCount(int qId)throws AppException{
		int acount = 0;
		try {
			acount = aMapper.selectByqidCount(qId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new AppException("数据异常",e);
		}
		return acount;
	}
	
	//根据qId查找所有的收藏并计数
		public int selectByqidCollect(int qId)throws AppException{
			int ccount = 0;
			try {
				ccount = cMapper.selectByqidCollect(qId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new AppException("数据异常",e);
			}
			return ccount;
		}
	//根据条件查询问题
		public List<Questions> selectByTJ(String qTitle)throws AppException{
			List<Questions> qlist = null;
			try {
				qlist = questionmapper.selectByTJ(qTitle);
				System.out.println("qlist:"+qlist.size());
				if(qlist.size()>0){
					for(int i=0;i<qlist.size();i++){
						String name = uMapper.selectByPrimaryKey(qlist.get(i).getuId()).getuName();
						qlist.get(i).setuName(name);
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new AppException("数据异常",e);
			}
			return qlist;
		}
}
